<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Connect events from a COM object to a PHP object</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.com-create-guid.html">« com_create_guid</a></li>
      <li style="float: right;"><a href="function.com-get-active-object.html">com_get_active_object »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.com.html">COM 函数</a></li>
    <li>Connect events from a COM object to a PHP object</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.com-event-sink" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">com_event_sink</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.2.0, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">com_event_sink</span> &mdash; <span class="dc-title">Connect events from a COM object to a PHP object</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.com-event-sink-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>com_event_sink</strong></span>(<span class="methodparam"><span class="type"><a href="class.variant.html" class="type variant">variant</a></span> <code class="parameter">$variant</code></span>, <span class="methodparam"><span class="type">object</span> <code class="parameter">$sink_object</code></span>, <span class="methodparam"><span class="type"><span class="type">array</span>|<span class="type">string</span>|<span class="type">null</span></span> <code class="parameter">$sink_interface</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   Instructs COM to sink events generated by
   <code class="parameter">variant</code> into the PHP object
   <code class="parameter">sink_object</code>.
  </p>
  <p class="para">
   Be careful how you use this feature; if you are doing something similar
   to the example below, then it doesn&#039;t really make sense to run it in a
   web server context.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.com-event-sink-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">variant</code></dt>

     <dd>

      <p class="para">
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">sink_object</code></dt>

     <dd>

      <p class="para">
       <code class="parameter">sink_object</code> should be an instance of a class with
       methods named after those of the desired dispinterface; you may use
       <span class="function"><a href="function.com-print-typeinfo.html" class="function">com_print_typeinfo()</a></span> to help generate a template class
       for this purpose.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">sink_interface</code></dt>

     <dd>

      <p class="para">
       PHP will attempt to use the default dispinterface type specified by
       the typelibrary associated with <code class="parameter">variant</code>, but
       you may override this choice by setting
       <code class="parameter">sink_interface</code> to the name of the dispinterface
       that you want to use.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.com-event-sink-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>true</code></strong>， 或者在失败时返回 <strong><code>false</code></strong>。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.com-event-sink-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       <code class="parameter">sink_interface</code> is nullable now.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.com-event-sink-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5375">
    <p><strong>示例 #1 COM event sink example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">IEEventSinker&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$terminated&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">ProgressChange</span><span style="color: #007700">(</span><span style="color: #0000BB">$progress</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$progressmax</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Download&nbsp;progress:&nbsp;</span><span style="color: #0000BB">$progress</span><span style="color: #DD0000">&nbsp;/&nbsp;</span><span style="color: #0000BB">$progressmax</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">DocumentComplete</span><span style="color: #007700">(&amp;</span><span style="color: #0000BB">$dom</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$url</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Document&nbsp;</span><span style="color: #0000BB">$url</span><span style="color: #DD0000">&nbsp;complete\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">OnQuit</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Quit!\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">terminated&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">$ie&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">COM</span><span style="color: #007700">(</span><span style="color: #DD0000">"InternetExplorer.Application"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$sink&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">IEEventSinker</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">com_event_sink</span><span style="color: #007700">(</span><span style="color: #0000BB">$ie</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$sink</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"DWebBrowserEvents2"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$ie</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">Visible&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$ie</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">Navigate</span><span style="color: #007700">(</span><span style="color: #DD0000">"http://www.example.org"</span><span style="color: #007700">);<br />while(!</span><span style="color: #0000BB">$sink</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">terminated</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;</span><span style="color: #0000BB">com_message_pump</span><span style="color: #007700">(</span><span style="color: #0000BB">4000</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">$ie&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.com-event-sink-notes">
  <h3 class="title">注释</h3>
  <div class="caution"><strong class="caution">警告</strong>
   <p class="para">
    Prior to PHP 8.0.0, calling <span class="function"><a href="function.exit.html" class="function">exit()</a></span> from any of the event
    handlers is not supported, and may cause PHP to hang. This can be worked around
    by throwing an exception from the event handler, catching the exception in
    the main code, and calling <span class="function"><a href="function.exit.html" class="function">exit()</a></span> from there.
   </p>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.com-event-sink-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.com-print-typeinfo.html" class="function" rel="rdfs-seeAlso">com_print_typeinfo()</a> - Print out a PHP class definition for a dispatchable interface</span></li>
    <li class="member"><span class="function"><a href="function.com-message-pump.html" class="function" rel="rdfs-seeAlso">com_message_pump()</a> - Process COM messages, sleeping for up to timeoutms milliseconds</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>